package com.log4jviewer.ui.views;

import java.io.File;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.log4jviewer.Activator;
import com.log4jviewer.ErrorsContainer;

/**
 * Class represents a View where Log4j-viewer errors are displayed.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class ErrorView extends ViewPart {

    public static final String ID = "log4jviewer.ui.views.ErrorView";

    private static final Image ERROR_IMAGE = Activator.getImageDescriptor("icons" + File.separator + "error_level.png")
            .createImage();

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static TableViewer tableViewer;

    private ErrorsContainer errorsContainer;

    public static TableViewer getTableViewer() {
        return tableViewer;
    }

    public ErrorsContainer getErrorManager() {
        return errorsContainer;
    }

    public void setErrorManager(final ErrorsContainer errorsContainer) {
        this.errorsContainer = errorsContainer;
    }

    @Override
    public void createPartControl(final Composite parent) {
        tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        tableViewer.setUseHashlookup(true);
        tableViewer.setContentProvider(new ArrayContentProvider());

        Table table = tableViewer.getTable();
        table.setHeaderVisible(true);
        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
        table.setLayoutData(gridData);

        createColumns();
    }

    @Override
    public void setFocus() {
        getViewSite().getPage().activate(this);
        logger.debug("ErrorView in Focus.");
    }

    protected void createColumns() {
        // '!' (error image) column
        final String errorImageColumnTitle = "!";
        final int errorImageColumnWidth = 30;
        TableViewerColumn column = createSingleColumn(tableViewer, errorImageColumnTitle, errorImageColumnWidth);
        column.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(final Object element) {
                return null;
            }

            @Override
            public Image getImage(final Object element) {
                return ERROR_IMAGE;
            }
        });

        // 'Error's description' column
        final String errorDescriptionColumnTitle = "Error's description";
        final int errorDescriptionColumnWidth = 100;
        column = createSingleColumn(tableViewer, errorDescriptionColumnTitle, errorDescriptionColumnWidth);
        column.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(final Object element) {
                String error = (String) element;
                return error;
            }
        });
        column.setEditingSupport(new EditingSupport(tableViewer) {
            @Override
            protected CellEditor getCellEditor(final Object element) {
                return new TextCellEditor(tableViewer.getTable());
            }

            @Override
            protected boolean canEdit(final Object element) {
                return true;
            }

            @Override
            protected Object getValue(final Object element) {
                return element;
            }

            @Override
            protected void setValue(final Object element, final Object value) {
                // no code
            }
        });
    }

    private TableViewerColumn createSingleColumn(final TableViewer tableViewer, final String columnTitle,
            final int columnWidth) {
        TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, SWT.NULL);
        TableColumn column = viewerColumn.getColumn();
        column.setText(columnTitle);
        column.setWidth(columnWidth);
        return viewerColumn;
    }

    public void clearErrorTable() {
        tableViewer.getTable().removeAll();
        logger.debug("Error table was cleared.");
    }

    public void showErrors() {
        tableViewer.setInput(errorsContainer.getErrors());
        logger.debug("Errors have been displayed.");
    }
}
